<script type="text/javascript">
    function disableSubmit() {
        $(":submit").prop("disabled", true);
    }
</script>

<?
function humanTiming($time)
{
    if ($time != null) {
        $time = time() - strtotime($time); // to get the time since that moment
        $anho = 31536000;
        $mes = 2592000;
        $semana = 604800;
        $dia = 86400;

        $value = "";
        if (floor($time / $anho) > 0) {
            $anhos = floor($time / $anho);
            $time = $time - $anhos * $anho;
            $value = $anhos . ' año' . $anhos > 1 ? 's' : '';
        }
        if (floor($time / $mes) > 0) {
            $meses = floor($time / $mes);
            $time = $time - $meses * $mes;
            $value .= ' ' . $meses . ' mes' . $meses > 1 ? 'es' : '';
        }
        if (floor($time / $semana) > 0) {
            $semanas = floor($time / $semana);
            $time = $time - $semanas * $semana;
            $value .= ' ' . $semanas . ' semana' . $semanas > 1 ? 's' : '';
        }
        if (floor($time / $dia) > 0) {
            $dias = floor($time / $dia);
            $value .= ' ' . $dias . ' dia' . $dia > 1 ? 's' : '';
        }
        return $value;
    } else {
        return;
    }
}

?>

<section class="ym-grid linearize-level-1">
    <table id="selectable">
        <thead>
        <tr>
            <?
            foreach ($this->list as $key => $value) {
                echo '<th>' . $key . '</th>';
            }?>
            <th>Tareas</th>
        </tr>
        </thead>
        <tbody>
        <?
        foreach ($this->entries as $entry):
            //TODO review for optimization
            ?>
        <tr id="<?=$entry->getId()?>">
            <?
            foreach ($this->list as $key => $value) {
                if (is_array($value)) {
                    $method = 'get' . ucfirst($value[1]);
                    echo '<td>' . $value[0]($entry->$method()) . '</td>';
                } else {
                    if (($column = $entry->getAddedColumn($value)) != null) {
                        echo '<td>' . $column . '</td>';
                    } else {
                        $method = 'get' . ucfirst($value);
                        echo '<td>' . $entry->$method() . '</td>';
                    }
                }
            }

            ?>

            <td>
                <?if (!isset($this->showEditLink)): ?>
                <a href="<?=$this->url(array('controller' => $this->controller, 'action' => 'form', 'id' => $entry->id), 'default', true) ?>"><img
                    src="<?=$this->url?>img/reply.png"/></a>
                |
                <? endif?>
                <?if (!isset($this->showDeleteLink)): ?>
                <a href="<?=$this->url(array('controller' =>
                $this->controller, 'action' => 'remove', 'id' => $entry->id), 'default', true) ?>"><img
                    src="<?=$this->url?>img/action_delete.png"></a>
                <? endif?>
                <?
                //TODO show other tasks for this entity
                if (isset($this->tasks)) {
                    $url = '|';
                    if (isset($this->showEditLink) && isset($this->showDeleteLink)) {
                        $url = '';
                    }
                    foreach ($this->tasks as $task) {
                        $url .= '<a href="' . $task['url'];
                        if (isset($task['require_id'])) {
                            $url .= '/id/' . $entry->getId();
                        }
                        if (isset($task['param'])) {
                            $method = $task['method'];
                            $url .= '/' . $task['param'] . '/' . $entry->$method();
                        }
                        $url .= '">';
                        if (isset($task['img'])) {
                            $url .= '<img src="' . $this->url . 'img/' . $task['img'] . '">';
                        } else {
                            $url .= $task['label'];
                        }
                        $url .= '</a>';

                        echo $url;
                    }
                }
                ?>
            </td>
        </tr>
            <?
        endforeach;?>
        </tbody>
    </table>

    <?if (!isset($this->showNewLink)): ?>
    <a href="<?=$this->url(array('controller' => $this->controller, 'action' => 'form'), 'default', true) ?>">
        <img src="<?=$this->url?>img/action_add.png"/>
        crear nuevo</a>
    <? endif?>
</section>

